﻿using Iworld.Client.Framework.AuthorService;
using Iworld.ViewModel;

namespace Iworld.Client.Framework
{
    /// <summary>
    /// 修改二级密码视图模型
    /// </summary>
    public class EditTwoLevelPasswordViewModel : SubPageFrameViewModelBase
    {

        string oldTwoLevelPassword;
        string newTwoLevelPassword;
        string newTwoLevelPasswordCheck;
        /// <summary>
        /// 旧密码
        /// </summary>
        public string OldTwoLevelPassword
        {
            get
            {
                return oldTwoLevelPassword;
            }
            set
            {
                if (oldTwoLevelPassword != value)
                {
                    oldTwoLevelPassword = value;
                    this.OnPropertyChanged("OldTwoLevelPassword");
                }
            }
        }

        /// <summary>
        /// 新密码
        /// </summary>
        public string NewTwoLevelPassword
        {
            get
            {
                return newTwoLevelPassword;
            }
            set
            {
                if (newTwoLevelPassword != value)
                {
                    newTwoLevelPassword = value;
                    this.OnPropertyChanged("NewTwoLevelPassword");
                }
            }
        }

        /// <summary>
        /// 确认新密码
        /// </summary>
        public string NewTwoLevelPasswordCheck
        {
            get
            {
                return newTwoLevelPasswordCheck;
            }
            set
            {
                if (newTwoLevelPasswordCheck != value)
                {
                    newTwoLevelPasswordCheck = value;
                    this.OnPropertyChanged("NewTwoLevelPasswordCheck");
                }
            }
        }

        /// <summary>
        /// 提交命令
        /// </summary>
        public UniversalCommand SubmitCommand { set; get; }

        public EditTwoLevelPasswordViewModel()
            : base(PageCategory.用户信息.ToString(), Page.修改二级密码.ToString())
        {
            this.SubmitCommand = new UniversalCommand((param) =>
            {
                if (this.IsBusy) return;
                if (NewTwoLevelPasswordCheck != NewTwoLevelPassword)
                {
                    this.ShowMsg("两次输入的新二级密码不一致");
                    return;
                }
                AuthorServiceClient asc = new AuthorServiceClient();
                asc.EditTwoLevelPasswordCompleted += this.WrapEventHandler<EditTwoLevelPasswordCompletedEventArgs>(EditTwoLevelPasswordCompleted);
                asc.EditTwoLevelPasswordAsync(new EditTwoLevelPasswordImport() { 
                 NewTwoLevelPassword = this.NewTwoLevelPassword
                 , 
                 OldTwoLevelPassword=this.OldTwoLevelPassword
                 ,
                 Token =  DataManager.GetValue<string>(DataKey.Iworld_Token)
                
                });
                this.IsBusy = true;
            });
        }

        void EditTwoLevelPasswordCompleted(object sender, EditTwoLevelPasswordCompletedEventArgs e)
        {
            this.IsBusy = false;
            if (e.Result.Success)
            {
                this.ShowMsg("修改密码成功");
            }
            else
            {
                this.ShowError(e.Result.Error);
            }
        }
    }
}
